//
//  MyDocument.m
//  WebClipper
//
//  Created by Daniel Braun on 21/02/07.
//  CopyrightDaniel Braun http://page-appart.net, http://braun.daniel.free.fr  2007 . All rights reserved.
//

#import "MyDocument.h"
#import <WebKit/WebView.h>
#import <WebKit/WebFrame.h>
#import <WebKit/WebResource.h>
#import <WebKit/WebDataSource.h>
#import <WebKit/DOM.h>

#import "DOMHTMLHtmlElementExtensions.h"
#import "NSTreeExt.h"
#import "WCWebExtractSet.h"
#import "WCExtractSetArray.h"
#import "WCWebFetchController.h"
#import "WCWebLoadProtocol.h"

@implementation MyDocument

- (id)init
{
    self = [super init];
    if (self) {
	    esets=[[WCExtractSetArray alloc]init]; 
	    WCWebExtractSet *e=[[WCWebExtractSet alloc] init];
	    [esets addObject:[e autorelease]];
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
    
    }
    return self;
}

- (void) dealloc {
	[esets release];
	[super dealloc];
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}


- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
	NSAssert(webController, @"webcontroler not connected");
	//[webController setEset:eset];
	[webController bind:@"eset" toObject:esetController
       withKeyPath:@"selection.self"
	   options:nil];
	[webController bind:@"curitem" toObject:itemCtrl
		withKeyPath:@"selection.self"
		    options:nil];
	if (loadedEsets) {
		[self _processAndLoadNext:webController private:nil];
	}
	//NSLog(@"XXXX main win %d\n", [[[NSApplication sharedApplication]mainWindow] windowNumber]);
	//NSLog(@"XXXX doc win %d\n", [[self windowForSheet] windowNumber]);
}

- (NSData *)dataRepresentationOfType:(NSString *)aType
{
	// FLD
	NSDictionary *info=[NSDictionary dictionaryWithObjectsAndKeys:
						   esets ? (id)esets : (id)[NSNull null], @"esets",
		nil];
	NSData *d=[NSKeyedArchiver archivedDataWithRootObject: info];
	return d;
	
    return nil;
}

- (void) _processAndLoadNext:(NSObject <WCWebLoadProtocol> *)ctrler private:p
{
	WCWebExtractSet *es;
	NSEnumerator *en;
	if (!p) {
		en=[esets objectEnumerator];
		es=nil;
	} else {
		es=[p objectAtIndex:0];
		en=[p objectAtIndex:1];
	}
	if (es) {
		[es processExtractionWithController:ctrler];
	}
	es=[en nextObject];
	if (!es) {
		int n=[esets count];
		[esetController setSelectionIndex:n-1];
		return;
	}
	[webController setEset:es];
	[webController loadPageAndPerform:@selector(_processAndLoadNext:private:) target:self 
				  private:[NSArray arrayWithObjects:es, en, nil]];
}
		
		
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
	//NSLog(@"XXXX main win %d\n", [[[NSApplication sharedApplication]mainWindow] windowNumber]);
	//NSLog(@"XXXX doc win %d\n", [[self windowForSheet] windowNumber]);

	NSDictionary *info = [NSKeyedUnarchiver unarchiveObjectWithData:data];
	if (!info) return NO;
	[esets release];
	esets=nil;
	esets=[[WCExtractSetArray extractSetArrayFromSaveDictionnary:info]retain];
	loadedEsets=YES;
	return YES;
}

- (NSMutableArray*)esets
{
	NSAssert([esets isKindOfClass:[WCExtractSetArray class]], @"bad eset class");
	return (NSMutableArray*) esets;
}
- (WCExtractSetArray*)_esets
{
	NSAssert([esets isKindOfClass:[WCExtractSetArray class]], @"bad eset class");
	return esets;
}
- (void) setEsets:(NSArray *)e
{
	NSAssert([esets isKindOfClass:[WCExtractSetArray class]], @"bad eset class");
	[esets release];
	esets=[e retain]; 
}

- (void) addExtractionSet:(WCWebExtractSet *)es
{
	//NSLog(@"XXXX main win %d\n", [[[NSApplication sharedApplication]mainWindow] windowNumber]);
	//NSLog(@"XXXX doc win %d\n", [[self windowForSheet] windowNumber]);

	[esetController addObject:es];
	[esetController setSelectedObjects:[NSArray arrayWithObject:es]];
}

- (void) curpathUpdated
{
	[webController curpathUpdated];
}




@end

